<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 11/21/14
 * Time: 9:24 AM
 */

namespace Frontend;

use MyModel,
    Sentry,
    Input,
    Str,
    View,
    Blog,
    BaseController,
    Redirect;

class BlogsController extends BaseController
{


    public function getIndex()
    {
        $data = array();
        $myModel = new MyModel();
        $data['new_blog'] = $myModel->listTop("Blog", array('top' => 'created_at', 'take' => 10));
        $data['hot_blog'] = $myModel->listTop("Blog", array('top' => 'created_at', 'take' => 4, 'conditions' => array(array('field' => 'is_hot', 'opr' => '=', 'value' => 1))), 'is_hot');
        $data['views_blog'] = $myModel->listTop('Blog', array('top' => 'views', 'take' => 5));
        $data['categories'] = $myModel->listTop('Bcategory', array('top' => 'id'));
        return View::make('frontend/blogs/index', compact('data'));
    }

    public function getCategory($id, $slug)
    {
        if (is_null($data['category'] = \Bcategory::find($id)))
            \App::abort('404');
        $myModel = new MyModel();
        $data['new_blog'] = $myModel->listTop("Blog", array('top' => 'created_at', 'take' => 10));
        $data['views_blog'] = $myModel->listTop('Blog', array('top' => 'views', 'take' => 5));
        $data['categories'] = $myModel->listTop('Bcategory', array('top' => 'id'));
        $data['blogs'] = $data['category']->blogs()->paginate(10);
        return View::make('frontend/blogs/category', compact('data'));
    }

    public function getDetail($id, $slug)
    {
        if (is_null($data['blog'] = Blog::find($id)))
            return \App::abort('404');
        $myModel = new MyModel();
        $data['new_blog'] = $myModel->listTop("Blog", array('top' => 'created_at', 'take' => 10));
        $data['views_blog'] = $myModel->listTop('Blog', array('top' => 'views', 'take' => 5));
        $data['categories'] = $myModel->listTop('Bcategory', array('top' => 'id'));

        return View::make('frontend/blogs/details', compact('data'));
    }
} 